←Select platform

WriteJavaScript(stream,string,IList<PDFJavaScript>,stream) Method

Summary

Writes (or removes) JavaScript actions to an output stream.

Syntax
C#
C++/CLI
public static void WriteJavaScript( 
   stream sourceStream, 
   string password, 
   IList<PDFJavaScript> Script_, 
   stream destinationStream 
) 
public static void WriteJavaScript( 
   Stream^ sourceStream, 
   String^ password, 
   IList<PDFJavaScript>^ Script_, 
   Stream^ destinationStream 
) 

Parameters

sourceStream

Stream containing the source PDF.

password

The password to use if sourceStream contains an encrypted PDF.

javaScript

List of WriteJavaScript actions to be written. If null or the array is empty, the function deletes the JavaScript from the file.

destinationStream

Stream that will contain the output PDF.

Remarks

If javaScript is not null and contains valid JavaScript actions, WriteJavaScript will:

  1. Copy the PDF file from sourceStream
  2. Add the JavaScript actions
  3. Write the result to destinationStream

If the source PDF file contains JavaScript, those operations will be replaced by the actions from javaScript. To add JavaScript actions to the existing JavaScript actions from the source file, follow these steps:

  1. Call PDFFile.ExtractJavaScript(stream,string) to read the existing JavaScript actions
  2. Add your actions to the existing list
  3. Write all the actions (existing and new) to the output stream using WriteJavaScript.

If javaScript is null, WriteJavaScript will remove any JavaScript from the file.

There are several variations of this function, which take the source from a disk file or a stream and write the output to a disk file or a stream.

The actions from javaScript will be sorted in ascending order by the PDFJavaScript.Name property. This means the order in which the JavaScript actions are written to the ourput and executed by the JavaScript interpreter is not necessarily the order in which you add them to the javaScript list. See PDFJavaScript.Name for more details.

The example below demonstrates the result of this sort. The example adds two actions named name2 and name1. The write however, changes the order and writes them in the order name1, name2. So when you load the resulting PDF file in a viewer that supports JavaScript (eg: web browser), the messages are displayed in reverse order.

Example
C#
using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
 
 
public void TestWriteJavaScript_Stream_string_IList_Stream() 
{ 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "leadtools.pdf"); ; 
   string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, @"out.pdf"); 
   List<PDFJavaScript> list = new List<PDFJavaScript>(2); 
   PDFJavaScript item; 
 
   item.Name = "name2"; 
   item.Code = "app.alert(\"Hello 2\");"; 
   list.Add(item); 
 
   item.Name = "name1"; 
   item.Code = "app.alert(\"Hello 1\");"; 
   list.Add(item); 
 
   using (FileStream srcStream = File.Open(srcFileName, FileMode.Open)) 
   using (FileStream destStream = File.Create(dstFileName)) 
      PDFFile.WriteJavaScript(srcStream, null, list, destStream); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.7.10
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.